package com.cat.chapter037;

/**
 * @author cat
 * @description https://leetcode.cn/problems/house-robber-iii/
 * @create 2025/6/24 19:00
 * @since JDK17
 */

public class Solution06 {
    int yes = 0;
    int no = 0;

    public int rob(TreeNode root) {
        f(root);
        return Integer.max(yes, no);
    }

    void f(TreeNode root) {
        if (root == null) {
            yes = 0;
            no = 0;
            return;
        }
        int y = root.val;
        int n = 0;
        f(root.left);
        y += no;
        n += Integer.max(yes, no);
        f(root.right);
        y += no;
        n += Integer.max(yes, no);

        yes = Integer.max(y, yes);
        no = Integer.max(n, no);
    }

}
